home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 293_01 / cut.c < prev    next >
C/C++ Source or Header  |  1989-08-23  |  8KB  |  229 lines

  1. /************************ cut.c *************************************
  2.  
  3.       3-D Reconstruction of Medical Images
  4.  
  5.     Three Dimensional Reconstruction Of Medical
  6.     Images from Serial Slices - CT, MRI, Ultrasound
  7.  
  8.  
  9.    These programs process a set of slices images (scans) for one
  10.    patient. It outputs two sets of files containing nine predefined
  11.    views of bony surfaces. One set contains distance values and
  12.    the other gradient values.
  13.  
  14.    The distance values are used as 3-D spatial topographic surface
  15.    coordinate maps for geometrical analysis of the scanned object.
  16.  
  17.    The gradient values are used for rendering the surface maps on
  18.    CRT displays for subjective viewing where perception of small
  19.    surface details is important.
  20.  
  21.     Daniel Geist, B.S.
  22.     Michael W. Vannier, M.D.
  23.  
  24.     Mallinckrodt Institute of Radiology
  25.     Washington University School of Medicine
  26.     510 S. Kingshighway Blvd.
  27.     St. Louis, Mo. 63110
  28.  
  29.     These programs may be copied and used freely for non-commercial
  30.     purposes by developers with inclusion of this notice.
  31.  
  32.  
  33. ********************************************************************/
  34. #include <stdio.h>
  35. #include <math.h>
  36. /* some global variables*/
  37. int FIRSTSLICE,LASTSLICE,THRESHOLD,NLINES;
  38. float ZOOM;
  39. int  buffer[2][6][256];          /* input buffer */
  40. float fxbuf[5][256],fybuf[5][256]; /* X,Y Views floating buffers */ 
  41. char *fnamein="ctbild.000";        /* input file name  */
  42.  
  43. succ(i)   /* (i+1) modolus 3 */
  44. int i;
  45. {return(i==2?0:i+1);}
  46.  
  47. prev(i)   /* (i-1) modolus 3 */
  48. int i;
  49. {return(i==0?2:i-1);}
  50.  
  51. /* Set input file - add slice number to extension */
  52. setfilename(filenum)
  53. int filenum;
  54. {fnamein[7]=filenum/100+'0';
  55.  fnamein[8]=(filenum%100)/10+'0';
  56.  fnamein[9]='0'+filenum%10;
  57. }
  58.  
  59. /* interpolate from bottom line to top line n-1 lines  */
  60. interpolate(line,bot,top,n)
  61. int line,bot,top,n;
  62. {int next,i,j,x,inc;
  63.  inc=top>bot?1:(-1);    /* interpolate backward or forwards ? */
  64.  next=bot+inc;
  65.  for(i=1,j=n-1;i<n;i++,j--){   /* do for each next line of interpolation */
  66.       for(x=0;x<256;x++) buffer[line][next][x]=
  67.         (buffer[line][bot][x]*j+buffer[line][top][x]*i)/n;
  68.       next+=inc;
  69.  }
  70. }
  71.  
  72. /* midpoint - fraction part of threshold transition distance */ 
  73. float midpoint(b,a)
  74. float b,a;
  75. {return( (THRESHOLD-a) / (b-a) );}
  76.  
  77. /* get floting point distance values  */
  78. getdistances()
  79. {int z,x,y,i,j,start,stop,inc,line,rzoom,inter;
  80.  float remain;
  81.  FILE *fxfloat,*fyfloat,*fzfloat,*fn[2];
  82.   NLINES=0;         /* number of output lines in X,Y view directions */
  83.   remain=0;         /* remainder of interpolation after roundoff */
  84.   rzoom=ZOOM+0.5;   /* rounded zoom factor */
  85.   fxfloat=fopen("xdis.dat","wb");    /* X view floating output file */
  86.   for(z=0;z<(LASTSLICE-FIRSTSLICE);z++){  /* For each Slice */
  87.       for(i=0;i<2;i++){               /* open next two slice files */
  88.           setfilename(FIRSTSLICE+z+i);
  89.           fn[i]=fopen(fnamein,"rb");
  90.           fseek(fn[i],(long)512,SEEK_SET); /* skip header block */
  91.       } 
  92.       inter=rzoom; /* interpolation factor assumed rounded zoom */
  93.       /* correct interpolation factor according to floating remainder */
  94.       remain+=rzoom-ZOOM;
  95.       if(remain>=1){
  96.           inter-=1;
  97.           remain-=1;
  98.       }
  99.       else if(remain<=(-1)){
  100.           inter+=1;
  101.           remain+=1;
  102.       }
  103.  
  104.       line=0;               /* current input buffer line */
  105.       for(j=0;j<inter;j++)  /* clear X,Y floating buffers */
  106.        for(i=0;i<256;i++)fxbuf[j][i]=256;
  107.       for(y=0;y<256;y++){              /* For each line */
  108.           fread(buffer[line][0],1,512,fn[0]);   
  109.           fread(buffer[line][inter],1,512,fn[1]);
  110.           interpolate(line,0,inter,inter); /* interpolate in_between */
  111.           for(i=0;i<inter;i++){   /* For each interplation line */
  112.              for(x=128;x<256;x++)  /* For each Voxel value */
  113.                /* find threshold transition*/
  114.               if (buffer[line][i+1][x]>=THRESHOLD){
  115.                 /* if first transition in X direction get floating distance */
  116.                 if(fxbuf[i][y]==256.0) fxbuf[i][y]=(x==128)?0:x-1+
  117.                       midpoint((float)buffer[line][i+1][x],
  118.                               (float)buffer[line][i+1][x-1]);
  119.              }
  120.           }
  121.           line=1-line;  /* change current input buffer line */
  122.       }
  123.       NLINES+=inter;           /* increment number of output lines */  
  124.       fclose(fn[0]);           /* close slice files */
  125.       fclose(fn[1]);
  126.       fwrite(fxbuf,1,inter*1024,fxfloat); /* write output lines to files */
  127.       printf("did %d \n",z);
  128.   }
  129.   fclose(fxfloat); /* close X,Y floating files */
  130.   /* write Z floating files */
  131. }
  132.  
  133. unsigned char grad(y1,y2,z1,z2,y_factor,z_factor)
  134. float y1,y2,z1,z2;
  135. int y_factor,z_factor;
  136. {float gx,gy,gz;
  137.  unsigned char gxint;
  138.      /* get z and y components of gradient */
  139.   gz=(z1-z2)/y_factor;
  140.   gy=(y1-y2)/z_factor;
  141.      /*compute gx - normalized x component of gradient */
  142.   gx=1/sqrt(1+gz*gz+gy*gy);
  143.   gxint=255*gx+0.5;      /*scale gx by 256 and round */
  144.   return(gxint);
  145. }
  146.  
  147. /* get gradient and depth shades for one image line */
  148. doline(lineg,lined,line,prevline,succline,z_factor,fg,fd)
  149. unsigned char lineg[],lined[];  /*output buffers */
  150. int line,prevline,succline;  /* input buffer configuration */
  151. int z_factor; /* for choosing forward, backward or central differences */
  152. FILE *fg,*fd;  /* output files */
  153. {int i;
  154.   /* do first pixel in line */
  155.   if(fxbuf[line][0]==256)lineg[0]=lined[0]=0;
  156.   else{
  157.       lined[0]=255-fxbuf[line][0]; /*distance shade */
  158.       lineg[0]=grad(fxbuf[line][0],fxbuf[line][1],fxbuf[prevline][0],
  159.                        fxbuf[succline][0],1,z_factor);
  160.   }
  161.   /* do rest of pixels inside line */
  162.   for(i=1;i<255;i++) if(fxbuf[line][i]==256)lineg[i]=lined[i]=0;
  163.   else{
  164.       lined[i]=255-fxbuf[line][i]; /*distance shade */
  165.       lineg[i]=grad(fxbuf[line][i-1],fxbuf[line][i+1],fxbuf[prevline][i],
  166.                        fxbuf[succline][i],2,z_factor);
  167.   }
  168.   /* do last pixel in line */
  169.   if(fxbuf[line][255]==256)lineg[255]=lined[255]=0;
  170.   else{
  171.       lined[255]=255-fxbuf[line][255]; /*distance shade */
  172.       lineg[255]=grad(fxbuf[line][255],fxbuf[line][254],fxbuf[prevline][255],
  173.                        fxbuf[succline][255],1,z_factor);
  174.   }
  175.   fwrite(lineg,1,256,fg); /* write to output files */
  176.   fwrite(lined,1,256,fd);
  177. }
  178.  
  179. /* create an gradient and distance shaded view */
  180. doviews(namedis,nameg,named,nlines)
  181. char *namedis,*nameg,*named; /* floating file , gradiet and distance files*/
  182. int nlines;                  /* number of lines in image */
  183. {FILE *fg,*fd,*ffloat;
  184.  int z,i,j,k,midline;
  185.  char lined[256],lineg[256]; /* gradient and distance value buffers */
  186.  midline=1;                  /* middle line in input buffer */
  187.  fd=fopen(named,"wb");       /* open output and input files */
  188.  fg=fopen(nameg,"wb");
  189.  ffloat=fopen(namedis,"rb");
  190.  fread(fxbuf,1,3072,ffloat); /* read first three floating lines */
  191.  /* do first line */
  192.  doline(lineg,lined,0,0,1,1,fg,fd);
  193.  /* do rest of lines */
  194.  for(z=0;z<(nlines-2);z++){      /*for each inside line */
  195.      doline(lineg,lined,midline,prev(midline),succ(midline),2,fg,fd);
  196.      fread(fxbuf[prev(midline)],1,1024,ffloat); /*read next floating line */
  197.      midline=succ(midline);
  198.      printf(" did %d \n",z);
  199.  }
  200.  /* do last line */
  201.  doline(lineg,lined,midline,prev(midline),midline,1,fg,fd);
  202.  fclose(fg);  /* close all files */
  203.  fclose(fd);
  204.  fclose(ffloat);
  205. }
  206.  
  207. /**********************************************************/
  208. /**** MAIN ***** MAIN ***** MAIN ***** MAIN ***** MAIN ****/
  209. /**********************************************************/
  210. main()
  211. {
  212.  /* first get some parameters from user */
  213.  printf("Enter Zoom factor: ");
  214.  scanf("%f",&ZOOM);
  215.  printf("Enter Starting scan number: ");
  216.  scanf("%d",&FIRSTSLICE);
  217.  printf("Enter ending scan number: ");
  218.  scanf("%d",&LASTSLICE);
  219.  printf("Enter threshold number: ");
  220.  scanf("%d",&THRESHOLD);
  221.  THRESHOLD+=1024;
  222.  
  223.  getdistances(); /* get floating distance values */
  224.  /* create images */
  225.  printf("doing right lateral (X) views\n");
  226.  doview